package com.example.test_web;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    TextView responseText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendrequest = (Button)findViewById(R.id.send_request);
        responseText = (TextView)findViewById(R.id.response_text);
        sendrequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequestURL();
            }
        });
    }

    private  void  sendRequestURL(){
       new Thread(new Runnable() {
           @Override
           public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url("http://172.18.41.117:8080/get_data.xml").build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseXMLWithPUll(responseData);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
           }
       }).start();
    }

    private void parseXMLWithPUll(String xmlData){
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(new StringReader(xmlData));
            int eventType = xmlPullParser.getEventType();
            String id = "";
            String name = "";
            String version = "";
            while (eventType!=XmlPullParser.END_DOCUMENT){
                String nodename = xmlPullParser.getName();
                switch (eventType){
                    case XmlPullParser.START_TAG:{
                        if("id".equals(nodename)){
                            id = xmlPullParser.nextText();
                        }
                        else if("name".equals(nodename)){
                            name = xmlPullParser.nextText();
                        }
                        else if("version".equals(nodename)){
                            version = xmlPullParser.nextText();
                        }
                        break;
                    }
                    case XmlPullParser.END_DOCUMENT:{
                        if("app".equals(nodename)){
                            Log.d("Mainactivity", "id is"+id);
                            Toast.makeText(MainActivity.this,"id is "+id,Toast.LENGTH_SHORT).show();
                            Log.d("Mainactivity", "name is"+name);
                            //Toast.makeText(MainActivity.this,"name is "+name,Toast.LENGTH_SHORT).show();
                            Toast.makeText(MainActivity.this,"version is "+version,Toast.LENGTH_SHORT).show();
                            Log.d("Mainactivity", "version is"+version);
                        }
                        break;
                    }
                    default:break;
                }
                eventType = xmlPullParser.next();
            }
        } catch (XmlPullParserException | IOException e) {
            e.printStackTrace();
        }
    }

}
